require 'digest/sha1'
require 'digest/md5'
class User < ActiveRecord::Base
  has_many :schools, :dependent => :destroy
  has_one :profile, :dependent => :destroy
  has_many :broadcasts, :dependent => :destroy

  #validates_format_of :avatar_url, :with => /^.*(.jpg|.JPG|.gif|.GIF|.png|.PNG)$/
  file_column :avatar_url, :magick => {
                              :geometry => "72x72>",
                              :versions => {"small" => "16x16", "medium" => "32x32", "large" => "48x48"}
                            }
  
  # Virtual attribute for the unencrypted password
  attr_accessor :password
  #attr_reader :avatar_url

  validates_presence_of     :login, :email
  validates_presence_of     :password,                   :if => :password_required?
  validates_presence_of     :password_confirmation,      :if => :password_required?
  validates_length_of       :password, :within => 4..40, :if => :password_required?
  validates_confirmation_of :password,                   :if => :password_required?
  validates_length_of       :login,    :within => 3..40
  validates_length_of       :email,    :within => 3..100
  validates_uniqueness_of   :email, :case_sensitive => false
  before_save :encrypt_password
  
  # prevents a user from submitting a crafted form that bypasses activation
  # anything else you want your user to change should be added here.
  attr_accessible :login, :email, :password, :password_confirmation, 
                  :avatar_url, :delicious_url, :douban_url, :yupoo_url, :fanfou_url, :twitter_url
  
  # Authenticates a user by their login name and unencrypted password.  Returns the user or nil.
=begin
  def self.authenticate(login, password)
    u = find_by_login(login) # need to get the salt
    u && u.authenticated?(password) ? u : nil
  end
=end

  #Authenticate a user by their email and unencrypted password. Returns the user or nil.
  def self.authenticate(email, password)
    u = find_by_email(email)
    u && u.authenticated?(password) ? u : nil
  end
  
  # Encrypts some data with the salt.
  def self.encrypt(password, salt)
    Digest::SHA1.hexdigest("--#{salt}--#{password}--")
  end

  # Encrypts the password with the user salt
  def encrypt(password)
    self.class.encrypt(password, salt)
  end

  # Modify for phpBB users, by suave
  def authenticated?(password)
    #crypted_password == encrypt(password) || old_password == Digest::MD5.hexdigest(password)

    if !old_password.blank? && crypted_password == Digest::SHA1.hexdigest("--#{salt}--#{Digest::MD5.hexdigest(password)}--")
      self.old_password = nil
      save(false)
    end
    crypted_password == encrypt(password) || crypted_password == Digest::SHA1.hexdigest("--#{salt}--#{Digest::MD5.hexdigest(password)}--")
  end

  def remember_token?
    remember_token_expires_at && Time.now.utc < remember_token_expires_at 
  end

  # These create and unset the fields required for remembering users between browser closes
  def remember_me
    remember_me_for 2.weeks
  end

  def remember_me_for(time)
    remember_me_until time.from_now.utc
  end

  def remember_me_until(time)
    self.remember_token_expires_at = time
    self.remember_token            = encrypt("#{email}--#{remember_token_expires_at}")
    save(false)
  end

  def forget_me
    self.remember_token_expires_at = nil
    self.remember_token            = nil
    save(false)
  end

  protected
    # before filter 
    def encrypt_password
      return if password.blank?
      self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
      self.crypted_password = encrypt(password)
    end
    
    def password_required?
      crypted_password.blank? || !password.blank?
    end
end
